<html>
<head><meta charset="utf-8"><title>PR #86155 · project-ffi-unwind · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/index.html">project-ffi-unwind</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html">PR #86155</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="247708115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247708115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247708115">(Jul 30 2021 at 13:32)</a>:</h4>
<p>So--- I finally got around to reading <a href="https://github.com/rust-lang/rust/issues/86155">#86155</a>.</p>



<a name="247708118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247708118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247708118">(Jul 30 2021 at 13:33)</a>:</h4>
<p>It looks really great.</p>



<a name="247708146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247708146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247708146">(Jul 30 2021 at 13:33)</a>:</h4>
<p>I'm trying to figure out, <span class="user-mention" data-user-id="120791">@RalfJ</span>, if your concerns have been addressed</p>



<a name="247708456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247708456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247708456">(Jul 30 2021 at 13:35)</a>:</h4>
<p>As far as I can tell, the new pass:</p>
<ul>
<li>Identifies calls that can't unwind and removes their extra edges from the terminator</li>
<li>For cases that could unwind, but bodies that cannot, we add a terminator that makes the program abort</li>
</ul>
<p>So the semantics of a call for miri are:</p>
<ul>
<li>When you call a function, it is UB if that terminator indicates that it cannot unwind but it does</li>
<li>But if the terminator says that it can unwind, then...it can :)</li>
</ul>
<p>If you ask for the MIR immediately post build, we'll have plenty of terminators that indicate potential unwinding which will be optimized away later, but it's not really a "change in the semantics of MIR".</p>
<p>Does that sound right?</p>



<a name="247708613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247708613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247708613">(Jul 30 2021 at 13:36)</a>:</h4>
<p>(cc <span class="user-mention" data-user-id="116015">@Alex Crichton</span>)</p>



<a name="247711916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247711916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Crichton <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247711916">(Jul 30 2021 at 14:03)</a>:</h4>
<p>I think so yeah, sounds right to me!</p>



<a name="247722217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247722217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247722217">(Jul 30 2021 at 15:21)</a>:</h4>
<p>I'm going to r+</p>



<a name="247722230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247722230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247722230">(Jul 30 2021 at 15:21)</a>:</h4>
<p>not a 1-way door :)</p>



<a name="247722324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247722324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247722324">(Jul 30 2021 at 15:21)</a>:</h4>
<p>needs rebase, though</p>



<a name="247722691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247722691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Crichton <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247722691">(Jul 30 2021 at 15:24)</a>:</h4>
<p>oh oops done now</p>



<a name="247738840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247738840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247738840">(Jul 30 2021 at 17:34)</a>:</h4>
<blockquote>
<p>If you ask for the MIR immediately post build, we'll have plenty of terminators that indicate potential unwinding which will be optimized away later, but it's not really a "change in the semantics of MIR".</p>
</blockquote>
<p>I dont follow. I think there is a shift in the sematnics of MIR: when a "function that shouldnt be able to unwind" contains a terminator (from a fn call) that can unwind, this is <em>UB</em> at codegen time. But right after MIR building, this actually occurs in safe code. Ergo there must be a dialect change somewhere.</p>



<a name="247738933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247738933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247738933">(Jul 30 2021 at 17:35)</a>:</h4>
<p>This is not blocking the PR (in particular now that this shift happens very early in the pipeline), but needs to be adressed carefully if/when we ever precisely document MIR semantics. Cc <a href="https://github.com/rust-lang/rust/issues/86299">https://github.com/rust-lang/rust/issues/86299</a></p>



<a name="247744681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247744681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247744681">(Jul 30 2021 at 18:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/PR.20.2386155/near/247738840">said</a>:</p>
<blockquote>
<p>I dont follow. I think there is a shift in the sematnics of MIR: when a "function that shouldnt be able to unwind" contains a terminator (from a fn call) that can unwind, this is <em>UB</em> at codegen time. But right after MIR building, this actually occurs in safe code. Ergo there must be a dialect change somewhere.</p>
</blockquote>
<p>Hmm, I don't think it's UB at any point in time, unless that function actually unwinds. What am I missing?</p>



<a name="247744768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247744768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247744768">(Jul 30 2021 at 18:24)</a>:</h4>
<p>I'm not sure I know what "UB at codegen time" even means, actually</p>



<a name="247744845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247744845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247744845">(Jul 30 2021 at 18:25)</a>:</h4>
<p>regardless I agree we should note it in <a href="https://github.com/rust-lang/rust/issues/86299">#86299</a> or some other tracking issue</p>



<a name="247759145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247759145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247759145">(Jul 30 2021 at 20:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/PR.20.2386155/near/247744768">said</a>:</p>
<blockquote>
<p>I'm not sure I know what "UB at codegen time" even means, actually</p>
</blockquote>
<p>Sounds like it would be equivalent to C++'s IFNDR - The program is ill-formed, and the implementation could diagnose it and terminate compilation (or diagnose it for fun and keep compiling anyways), but it doesn't have to, and the resulting program would be free from the as-if rule period.</p>



<a name="247794673"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247794673" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247794673">(Jul 31 2021 at 09:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/210922-project-ffi-unwind/topic/PR.20.2386155/near/247744768">said</a>:</p>
<blockquote>
<p>I'm not sure I know what "UB at codegen time" even means, actually</p>
</blockquote>
<p>sorry, I meant it is UB in the operational semantics that describe program behavior at codegen time (in the dialect that MIR is written in at codegen time)</p>



<a name="247794677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247794677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247794677">(Jul 31 2021 at 09:03)</a>:</h4>
<p>as in, if its not UB, codegen is just wrong</p>



<a name="247794811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/247794811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#247794811">(Jul 31 2021 at 09:07)</a>:</h4>
<p>I am talking about code like this (assume <code>panic=abort</code>):</p>
<div class="codehilite"><pre><span></span><code>extern &quot;C+unwind&quot; { fn can_unwind() }

fn can_not_unwind() {
  bb0:
    Call can_unwind() [ ret -&gt; bb1 ] // implicit: unwinding is propagated

  bb1:
    Return
}
</code></pre></div>
<p>When we have this MIR at codegen time, we generate a <code>nounwind</code> attribute for <code>can_not_unwind</code>. so if <code>can_unwind</code> unwinds, we have UB.</p>
<p>However, we generate exactly that MIR from safe code. So in the MIR we initially generate, this cannot be UB.</p>
<p>Ergo the rules for UB changed some time in between.</p>



<a name="248424257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/248424257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#248424257">(Aug 05 2021 at 00:01)</a>:</h4>
<p>Apologies for asking a question with vast ignorance: in that code, where does the landing pad come into play? <code>can_not_unwind</code> really can't undwind, and if <code>can_unwind</code> does unwind, a landing pad in <code>can_not_unwind</code> needs to abort the process.</p>



<a name="248606191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/248606191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#248606191">(Aug 06 2021 at 12:53)</a>:</h4>
<blockquote>
<p>and if can_unwind does unwind, a landing pad in can_not_unwind needs to abort the process.</p>
</blockquote>
<p>yes it is my understanding that <span class="user-mention" data-user-id="116015">@Alex Crichton</span>'s PR adds a pass that will add such a landing pad</p>



<a name="248606324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/PR%20%2386155/near/248606324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/PR.20.2386155.html#248606324">(Aug 06 2021 at 12:54)</a>:</h4>
<p>so after that pass we know that no function called from a <code>nounwind</code> function can unwind, so we can change the MIR dialect to one where doing so would be UB. this, in turn, allows codegen to emit the code that it does for LLVM (where we would have such UB).</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>